package dobest.ray.com.jiongjiong.activity.action.selftest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.BaseActivity;
import dobest.ray.com.jiongjiong.activity.action.ActionPresenter;
import dobest.ray.com.jiongjiong.bean.PresenterData;
import dobest.ray.com.jiongjiong.bean.SelfTestDataBean;
import dobest.ray.com.jiongjiong.callback.PresenterCallBack;
import dobest.ray.com.jiongjiong.utils.ACacheHelper;
import dobest.ray.com.jiongjiong.views.NavigationBar;

/**
 * Created by Ray on 2016/3/29.
 * https://github.com/ray0807
 */
public class TestResultActivity extends BaseActivity<ActionPresenter> implements PresenterCallBack {

    public static final String TEST_RESULT_DATA_TAG = "TEST_RESULT_DATA_TAG";

    private static String INTENT_FLAG = "INTENT_flag";

    private int flag = 0;//0默认近视测试结果

    private TextView tv_left_eye_du;
    private TextView tv_left_eye_eva;

    private TextView tv_left_eye_du_this_time;
    private TextView tv_left_eye_eva_this_time;

    private TextView tv_right_eye_du;
    private TextView tv_right_eye_eva;

    private TextView tv_right_eye_du_this_time;
    private TextView tv_right_eye_eva_this_time;

    //远视
    private TextView tv_left_eye_du_far;
    private TextView tv_left_eye_eva_far;

    private TextView tv_left_eye_du_this_time_far;
    private TextView tv_left_eye_eva_this_time_far;

    private TextView tv_right_eye_du_far;
    private TextView tv_right_eye_eva_far;

    private TextView tv_right_eye_du_this_time_far;
    private TextView tv_right_eye_eva_this_time_far;

    //矫正
    private TextView tv_left_eye_correct_du;
    private TextView tv_left_eye_correct_eva;

    private TextView tv_left_eye_correct_du_this_time;
    private TextView tv_left_eye_correct_eva_this_time;

    private TextView tv_right_eye_correct_du;
    private TextView tv_right_eye_correct_eva;

    private TextView tv_right_eye_correct_du_this_time;
    private TextView tv_right_eye_correct_eva_this_time;


    public static void startTestResultActivity(Context context, int flag) {
        Intent intent = new Intent(context, TestResultActivity.class);
        intent.putExtra(INTENT_FLAG, flag);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_result);
        findViews();
        init();
        addListeners();
    }

    @Override
    public void findViews() {
        navi = (NavigationBar) findViewById(R.id.navi);
        navi.setTitle("检查结果");
        navi.setBackListener(R.mipmap.img_back, "");
        flag = getIntent().getIntExtra(INTENT_FLAG, 0);

        tv_left_eye_du = (TextView) findViewById(R.id.tv_left_eye_du);
        tv_left_eye_eva = (TextView) findViewById(R.id.tv_left_eye_eva);
        tv_left_eye_du_this_time = (TextView) findViewById(R.id.tv_left_eye_du_this_time);
        tv_left_eye_eva_this_time = (TextView) findViewById(R.id.tv_left_eye_eva_this_time);

        tv_right_eye_du = (TextView) findViewById(R.id.tv_right_eye_du);
        tv_right_eye_eva = (TextView) findViewById(R.id.tv_right_eye_eva);
        tv_right_eye_du_this_time = (TextView) findViewById(R.id.tv_right_eye_du_this_time);
        tv_right_eye_eva_this_time = (TextView) findViewById(R.id.tv_right_eye_eva_this_time);


        tv_left_eye_du_far = (TextView) findViewById(R.id.tv_left_eye_du_far);
        tv_left_eye_eva_far = (TextView) findViewById(R.id.tv_left_eye_eva_far);
        tv_left_eye_du_this_time_far = (TextView) findViewById(R.id.tv_left_eye_du_this_time_far);
        tv_left_eye_eva_this_time_far = (TextView) findViewById(R.id.tv_left_eye_eva_this_time_far);

        tv_right_eye_du_far = (TextView) findViewById(R.id.tv_right_eye_du_far);
        tv_right_eye_eva_far = (TextView) findViewById(R.id.tv_right_eye_eva_far);
        tv_right_eye_du_this_time_far = (TextView) findViewById(R.id.tv_right_eye_du_this_time_far);
        tv_right_eye_eva_this_time_far = (TextView) findViewById(R.id.tv_right_eye_eva_this_time_far);


        tv_left_eye_correct_du = (TextView) findViewById(R.id.tv_left_eye_correct_du);
        tv_left_eye_correct_eva = (TextView) findViewById(R.id.tv_left_eye_correct_eva);
        tv_left_eye_correct_du_this_time = (TextView) findViewById(R.id.tv_left_eye_correct_du_this_time);
        tv_left_eye_correct_eva_this_time = (TextView) findViewById(R.id.tv_left_eye_correct_eva_this_time);

        tv_right_eye_correct_du = (TextView) findViewById(R.id.tv_right_eye_correct_du);
        tv_right_eye_correct_eva = (TextView) findViewById(R.id.tv_right_eye_correct_eva);
        tv_right_eye_correct_du_this_time = (TextView) findViewById(R.id.tv_right_eye_correct_du_this_time);
        tv_right_eye_correct_eva_this_time = (TextView) findViewById(R.id.tv_right_eye_correct_eva_this_time);
    }

    @Override
    public void init() {
        setPresenter(new ActionPresenter(this), this);

        String uploadType = null;
        String downloadType = null;
        String valueL = null;
        String valueR = null;


        //近视
        if (flag == 0) {
            uploadType = "1";
            downloadType = "0";
            valueL = ACacheHelper.getInstance().getNearVisionLeft(this) + "";
            valueR = ACacheHelper.getInstance().getNearVisionRight(this) + "";

        } else if (flag == 1) {
            //远视
            uploadType = "2";
            downloadType = "0";
            valueL = ACacheHelper.getInstance().getNearVisionLeft(this) + "";
            valueR = ACacheHelper.getInstance().getNearVisionRight(this) + "";
        }

        getPresenter().getSelfTestVisionData(this, "158", uploadType, downloadType, valueL, valueR);
    }

    @Override
    public void addListeners() {

    }

    /**
     * 上传以后的自测数据
     */
    private SelfTestDataBean bean;

    @Override
    public <T> void callBackPresenter(PresenterData<T> data) {
        T t = data.data;
        if (t == null) {
            return;
        }
        if (TEST_RESULT_DATA_TAG.equals(data.tag)) {
            bean = (SelfTestDataBean) t;
            if (bean != null) {
                setData();
            }
        }
    }

    private void setData() {
        int nvLength = 0;
        int fvLength = 0;
        int correctLength = 0;
        if (bean.nearVision != null) {
            nvLength = bean.nearVision.size();
        }
        if (bean.farVision != null) {
            fvLength = bean.farVision.size();
        }
        if (bean.correctVision != null) {
            correctLength = bean.correctVision.size();
        }

        //近视
        if (nvLength == 1) {
            tv_left_eye_du_this_time.setText(bean.nearVision.get(nvLength - 1).left);
            tv_right_eye_du_this_time.setText(bean.nearVision.get(nvLength - 1).right);
            tv_left_eye_du.setText("-");
            tv_right_eye_du.setText("-");

        } else if (nvLength > 1) {
            tv_left_eye_du_this_time.setText(bean.nearVision.get(nvLength - 1).left);
            tv_right_eye_du_this_time.setText(bean.nearVision.get(nvLength - 1).right);
            tv_left_eye_du.setText(bean.nearVision.get(nvLength - 2).left);
            tv_right_eye_du.setText(bean.nearVision.get(nvLength - 2).right);
        }
        //远视
        if (fvLength == 1) {
            tv_left_eye_du_this_time_far.setText(bean.farVision.get(fvLength - 1).left);
            tv_right_eye_du_this_time_far.setText(bean.farVision.get(fvLength - 1).right);

            tv_left_eye_du_far.setText("-");
            tv_right_eye_du_far.setText("-");
        } else if (fvLength > 1) {
            tv_left_eye_du_this_time_far.setText(bean.farVision.get(fvLength - 1).left);
            tv_right_eye_du_this_time_far.setText(bean.farVision.get(fvLength - 1).right);

            tv_left_eye_du_far.setText(bean.farVision.get(fvLength - 2).left);
            tv_right_eye_du_far.setText(bean.farVision.get(fvLength - 2).right);
        }
        //矫正
        if (correctLength == 1) {
            tv_left_eye_correct_du_this_time.setText(bean.correctVision.get(correctLength - 1).left);
            tv_right_eye_correct_du_this_time.setText(bean.correctVision.get(correctLength - 1).right);

            tv_left_eye_correct_du.setText("-");
            tv_right_eye_correct_du.setText("-");
        } else if (correctLength > 1) {
            tv_left_eye_correct_du_this_time.setText(bean.correctVision.get(correctLength - 1).left);
            tv_right_eye_correct_du_this_time.setText(bean.correctVision.get(correctLength - 1).right);

            tv_left_eye_correct_du.setText(bean.correctVision.get(correctLength - 2).left);
            tv_right_eye_correct_du.setText(bean.correctVision.get(correctLength - 2).right);
        }


    }
}
